package com.kcht.itsed.mws.aleframework.biz;

import com.kcht.itsed.mws.aleframework.intf.IReaderCommandHelper;
import com.shhic.itsed.mws.deviceintf.*;

import java.util.*;

/**
 * 单个阅读器的命令合并工具
 * 目前只用于READ操作的合并
 * @author 张文硕
 *
 */
public class ReaderCommandOfSingleReaderHelper implements IReaderCommandHelper{
	
	private List<RawTagField> originalRawTagFieldList = new LinkedList<>();
	private List<AntennaProtocol> originalAntennaProtocolList = new LinkedList<>();
	
	public static IReaderCommandHelper getInstance(Set<ReaderCommand> rcSet) {
		return new ReaderCommandOfSingleReaderHelper(rcSet);
	}
	
	private ReaderCommandOfSingleReaderHelper(Set<ReaderCommand> rcSet) {
		for(ReaderCommand rc:rcSet){
			originalRawTagFieldList.addAll(rc.getReadTagFieldList());
			originalAntennaProtocolList.addAll(rc.getAntennaProtocolSet());
		}
	}
	
	@Override
	public Set<AntennaProtocol> generateCombinedAntennaProtocolSet() {
		Map<Integer,Set<String>> ants = new HashMap<>();
		originalAntennaProtocolList.forEach(ap->{
			if(!ants.containsKey(ap.getAntennaNo()))
				ants.put(new Integer(ap.getAntennaNo()), new HashSet<String>());
			ants.get(ap.getAntennaNo()).addAll(ap.getProtocols());
		});
		Set<AntennaProtocol> aps = new HashSet<>();
		for(Integer no : ants.keySet()){
			AntennaProtocol e = new AntennaProtocol();
			e.setAntennaNo(no);
			e.setProtocols(ants.get(no));
			aps.add(e);
		}
		return aps;
	}

	/**
	 * 标签区域遮蔽工具
	 * @author 张文硕
	 *
	 */
	private static class TagMarker{
		private RawTagBank bank;
		private boolean[] byteMark = new boolean[256];	//预留256个字节用于Mask
		public TagMarker(RawTagBank bank) {
			this.bank = bank;
		}
		
		//标记：注意单位是字节
		public void mark(int offset,int length){
			for(int i = offset;i<offset+length;i++)byteMark[i] = true;
		}
		
		//获得合并结果，返回的数组中为[offset,length]
		public List<int[]> combineResult(){
			List<int[]> r = new LinkedList<>();
			int start = -1,end = 0;
			for(int i=0;i<byteMark.length;i++){
				if(start == -1){
					if(!byteMark[i])continue;
					start = i;
				}else{
					if(byteMark[i]){
						end = i;
						continue;
					}else{
						r.add(new int[]{start,end-start+1});
						start = -1;
					}
				}
			}
			return r;
		}
		
		public RawTagBank getBank() {
			return bank;
		}
	}
	
	
	@Override
	public List<RawTagField> generateCombinedTagFieldSet() {
		Map<RawTagBank,TagMarker> tagMarkerMap = new HashMap<>();	//key是field
		for(RawTagField rtf:originalRawTagFieldList){
			if(!tagMarkerMap.containsKey(rtf.getBank()))tagMarkerMap.put(rtf.getBank(), new TagMarker(rtf.getBank()));
			tagMarkerMap.get(rtf.getBank()).mark((int)(rtf.getOffset()*rtf.getSizeUnit().byteRatio()),
					(int)(rtf.getLength()*rtf.getSizeUnit().byteRatio()));
		}
		List<RawTagField> res = new LinkedList<>();
		for(TagMarker tm:tagMarkerMap.values()){
			for(int[] r:tm.combineResult()){
				RawTagField rtf = new RawTagField();
				rtf.setBank(tm.getBank());
				if(r[0]%2 == 0 && r[1]%2 == 0){//单位用字
					rtf.setLength(r[1]/2);
					rtf.setOffset(r[0]/2);
					rtf.setSizeUnit(SizeUnit.WORD);
				}else{
					rtf.setLength(r[1]);
					rtf.setOffset(r[0]);
					rtf.setSizeUnit(SizeUnit.BYTE);
				}
				res.add(rtf);
			}
		}
		return res;
	}
}
